package com.thingcom.delinrobot.main.Launch.NewUser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.thingcom.delinrobot.Common.pjo.ChooseCountry;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.view.SimpleToolbar;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/27
 */
public class ChooseCountryBackFragment extends BaseBackFragment {

    private List<ChooseCountry> chooseCountries;
    private ChooseCountryAdapter chooseCountryAdapter;
    @BindView(R.id.toolbar_choose_country)
    SimpleToolbar toolbarChooseCountry;
    @BindView(R.id.recycler_choose_country)
    RecyclerView recyclerChooseCountry;
    @BindView(R.id.btn_choose_country)
    Button btnChooseCountry;
    private String chooseCountry;

    public static ChooseCountryBackFragment newInstance() {
        Bundle args = new Bundle();
        ChooseCountryBackFragment fragment = new ChooseCountryBackFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_choose_country, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarChooseCountry);
        initView();
        return view;
    }

    @OnClick(R.id.btn_choose_country)
    public void onViewClicked() {
        returnSelect();
    }

    private class ChooseCountryAdapter extends BaseQuickAdapter<ChooseCountry,BaseViewHolder> {
        ChooseCountryAdapter(@Nullable List<ChooseCountry> data) {
            super(R.layout.country_choose_item,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ChooseCountry item) {
            helper.setText(R.id.tv_country_name,item.getCountry());
            ImageView imageView = helper.getView(R.id.iv_country_yes);
            if (item.getClicks()){
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }

        }
    }

    private void initView(){
        chooseCountries = new ArrayList<>();
        chooseCountries.add(new ChooseCountry("Chinese mainland",false));
        chooseCountries.add(new ChooseCountry("Danish",false));
        chooseCountries.add(new ChooseCountry("Israel",false));
        chooseCountries.add(new ChooseCountry("Canada",false));
        chooseCountries.add(new ChooseCountry("Spain",false));
        chooseCountries.add(new ChooseCountry("Netherlands",false));
        chooseCountries.add(new ChooseCountry("British",false));
        chooseCountries.add(new ChooseCountry("Germany",false));
        chooseCountries.add(new ChooseCountry("Italy",false));
//        chooseCountries.add(new ChooseCountry("中国大陆7",false));
        recyclerChooseCountry.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
        chooseCountryAdapter = new ChooseCountryAdapter(chooseCountries);
        chooseCountryAdapter.bindToRecyclerView(recyclerChooseCountry);
        chooseCountryAdapter.setOnItemClickListener((adapter, view, position) -> {
                 for (int i=0;i<chooseCountries.size();i++){
                     chooseCountries.get(i).setClicks(false);
                 }
                 chooseCountries.get(position).setClicks(true);
                 chooseCountryAdapter.notifyDataSetChanged();
            chooseCountry = chooseCountries.get(position).getCountry();
        });

    }

    private void returnSelect(){
        Intent intent = new Intent();
        intent.putExtra(Constant.CHOOSE_COUNTRY,chooseCountry);

        Objects.requireNonNull(getTargetFragment()).onActivityResult(getTargetRequestCode(), Activity.RESULT_OK,intent);
        pop();
    }
}
